logo
Expand description
Simple and fast async channels

Lightweight async channel that can be used to implement futures, streams, notifiers, and actors.

Whisk defines a simple Channel type rather than splitting into sender / receiver pairs. A Channel can both send and receive. Rather than abstracting the implementation to use an Arc internally, the Arc is exposed externally. This allows for the programmer to avoid both wrapping in an additional Arc in cases where it would be required, and to create Weak references to the channel.

Optional Features

Getting Started

use whisk::{Channel, Chan, Stream};

enum Cmd {
    /// Tell messenger to add
    Add(u32, u32, Chan<u32>),
}

async fn worker_main(commands: Stream<Cmd>) {
    while let Some(command) = commands.recv().await {
        println!("Worker receiving command");
        match command {
            Cmd::Add(a, b, s) => s.send(a + b).await,
        }
    }

    println!("Worker stopping…");
}

async fn tasker_main() {
    // Create worker on new thread
    println!("Spawning worker…");
    let channel = Stream::from(Channel::new());
    let worker_thread = {
        let channel = channel.clone();
        std::thread::spawn(move || {
            let future = async move { worker_main(channel).await };
            pasts::Executor::default().spawn(future)
        })
    };

    // Do an addition
    println!("Sending command…");
    let oneshot = Chan::from(Channel::new());
    channel.send(Some(Cmd::Add(43, 400, oneshot.clone()))).await;
    println!("Receiving response…");
    let response = oneshot.recv().await;
    assert_eq!(response, 443);

    // Tell worker to stop
    println!("Stopping worker…");
    channel.send(None).await;
    println!("Waiting for worker to stop…");

    worker_thread.join().unwrap();
    println!("Worker thread joined");
}

// Call into executor of your choice
fn main() {
    pasts::Executor::default().spawn(tasker_main())
}

Structs

A Channel can send messages to itself, and can be shared between threads and tasks.

Type Definitions

Type alias for convenience
Type alias for convenience
Type alias for convenience
Type alias for convenience